//
//  NCFilterBox.swift
//  FilterBox
//
//  Created by 张文晏 on 2018/11/7.
//  Copyright © 2018 张文晏. All rights reserved.
//

import Foundation
import UIKit
import SnapKit

public class NCFilterBox {
    
    public var tabButtonContainer = UIView(
        frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 44)
    )
    
    public weak var owner: UIViewController?
    var singleItems = [FilterBoxSingleItem]()
    
    lazy var line: UIView = {
        let ln = UIView()
        ln.backgroundColor = UIColor(hex: 0xeeeeee)
        return ln
    }()
    
    public var showBottomLine = true { didSet { line.isHidden = !showBottomLine } }
    
    public init(owner: UIViewController) {
        self.owner = owner
        tabButtonContainer.backgroundColor = UIColor.white
    }
   
    public func set(items: [FilterBoxSingleItem]) {
        singleItems = items
        tabButtonContainer.subviews.forEach { $0.removeFromSuperview() }
        let width = tabButtonContainer.bounds.width / CGFloat(items.count)
        for (idx, item) in singleItems.enumerated() {
            item.filterTable.filterBox = self
            item.tabButton.filterBox = self
            item.tabButton.frame = CGRect(x: CGFloat(idx) * width, y: 0, width: width, height: 44)
            tabButtonContainer.addSubview(item.tabButton)
        }
        tabButtonContainer.addSubview(line)
        line.snp.remakeConstraints { make in
            make.leading.bottom.trailing.equalToSuperview()
            make.height.equalTo(1)
        }
    }
}

public extension NCFilterBox {
    public struct IdNameItem {
        public var id: Int
        public var name: String
        
        public init(id: Int, name: String) {
            self.id = id
            self.name = name
        }
    }
    
    public class FilterBoxSingleItem {
        var tabButton: NCFilterTabButton
        var filterTable: NCFilterTable
        
        public init(button: NCFilterTabButton, table: NCFilterTable, multiSelection: Bool = false) {
            tabButton = button
            filterTable = table
            filterTable.isMultiSelection = multiSelection
            tabButton.table = filterTable
            filterTable.filterTabButton = tabButton
        }
    }
}
